;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");

let url = "https://life-api.picc.com/ebsweb/pc/api/order/calcComboPrem";
let mysql = new db.mysqlClient("accident");
function genProductInfo() {
    let projects = [];
    plan = {
        "planA": {
            // 年龄段
            "year": ["18-30", "31-40", "41-50"],
            //保障期限
            "days": ["30y"],
            // 性别
            "gender": ["M", "F"],
            // 交费方式
            "interval": ["0", "3", "5", "10"],
            //主险基本保额
            "money": ["10000", "20000", "30000","40000","50000"],
            //意外医疗
            "accident_hospital": ["0", "20/天", "40/天", "60/天","80/天","100/天"],
            //意外伤残 
            "accident_death": ["60000"],
            //公共交通工具
            "common_traffic": ["300000"],
            //自驾意外
            "myselfDriver": ["600000"],
            // 航空意外
            "airplane_death": ["1500000"]

        },
        "planB": {
            // 年龄段
            "year": ["51-55","56-60"],
            //保障期限
            "days": ["30y"],
            // 性别
            "gender": ["M", "F"],
            // 交费方式
            "interval": ["0"],
            //主险基本保额
            "money": ["10000", "20000", "30000","40000","50000"],
            //意外医疗
            "accident_hospital": ["0"],
            //意外伤残 
            "accident_death": ["60000"],
            //公共交通工具
            "common_traffic": ["300000"],
            //自驾意外
            "myselfDriver": ["600000"],
            // 航空意外
            "airplane_death": ["1500000"]

        }
    };

    for (k in plan) {
        data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["gender"], plan[k]["interval"], plan[k]["money"], plan[k]["accident_hospital"], plan[k]["accident_death"], plan[k]["common_traffic"], plan[k]["myselfDriver"], plan[k]["airplane_death"]])
        for (i = 0; i < data.length; i++) {
            dt = helper.formatProductInfo(data[i], plan[k]);
            if (dt["interval"] == "0") {
                dt["payType"] = "一次交清";

            }
            else {
                dt["payType"] = "年交";

            }
            projects.push(dt);
            
        }


    }
    return projects;
}
function httpRequest(dt) {
    
    if (dt.interval == "0") {
        payend = "1Y"
    } else {
        payend = dt.interval + "Y"
    }

    if (dt.accident_hospital == "0") {

        params = '{"header":{"transRefGUID":"1231234342356767878","transDate":"2017-8-14","transTime":"11:29:00","businessType":"001"},"body":{"combCode":"C65483","policyType":"1","inputFactors":{"insuYear":"30Y","sex":"'+ dt.gender + '","payEndYear":"' + payend + '","age":"' + dt.year.split("-")[0] + '","amnt":"' + dt.money + '","combcode":"C65483","organid":"-1"},"addRisks":[{"riskCode":"","addRiskAmnt":""}]}}'

    }
    else {
        params = '{"header":{"transRefGUID":"1231234342356767878","transDate":"2017-8-14","transTime":"11:29:00","businessType":"001"},"body":{"combCode":"C65483","policyType":"1","inputFactors":{"insuYear":"30Y","sex":"'+ dt.gender + '","payEndYear":"' + payend + '","age":"' + dt.year.split("-")[0] + '","amnt":"' + dt.money + '","combcode":"C65483","organid":"-1"},"addRisks":[{"riskCode":"65503","addRiskAmnt":'+ dt.accident_hospital.replace("/天", "") + '}]}}'

    }
    return new Promise(function (resovle, reject) {
        request({
            url: url,
            method: "POST",
            headers: {
                "Accept": "application/json, text/plain",
                "Accept-Encoding": "gzip, deflate, br",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Content-Type": "application/json;charset=UTF-8",
                "Sec-Fetch-Dest": "empty",
                "Sec-Fetch-Mode": "cors",
                "Sec-Fetch-Site": "same-site",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"
            },
            rejectUnauthorized: false,
            body: (params)

        }, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                if (body.indexOf("试算参数输入错误！") == -1) {
                    price = JSON.parse(body).data.perPrem.toFixed(2);
                    rs = {
                        "name":"人保寿险百万身价两全保险（B款）产品组合",
                        "company":"中国人民保险",
                        "price":price.toString()
                    }
                    resovle( rs );
                }else{
                    console.log("计算保费出错");
                }
            }
        })

    })
    

}
var main = async function () {
    data = genProductInfo();
   
    for (i = 0; i < data.length; i++) {
        dt = data[i]
        // console.log( dt );
        let rs = await httpRequest(dt);
        let result = Object.assign( dt,rs );
        mysql.insertData( result );
    }
    mysql.close();
    
}
